import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
//导入css样式文件
import '@/styles/index.scss'
//引入elementui
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
//导入rem文件
import "./utils/rem.js"

import {postRequest} from './utils/api'
import {putRequest} from './utils/api'
import {getRequest} from './utils/api'
import {deleteRequest} from './utils/api'

//插件
Vue.prototype.postRequest = postRequest
Vue.prototype.putRequest = putRequest
Vue.prototype.getRequest = getRequest
Vue.prototype.deleteRequest = deleteRequest

//在vue中注册elementui
Vue.use(ElementUI);
Vue.config.productionTip = false


router.beforeEach((to, from, next) => {
  // console.log(to);
  // console.log(from);
  // console.log( sessionStorage.getItem('tokenStr'))
  if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
   if(sessionStorage.getItem('tokenStr')){ //判断sessionStorage是否存在token
    // alert("已经登录了")
    next();
   }else{
    //防止死循环
    // alert("暂时未登录")
    if (to.path === '/login') {
     next();
     return
    }else{
      next({
      path: '/login',
    });
  }
   }
  }
  else {
   next();
  }
  /*如果本地 存在 token 则 不允许直接跳转到 登录页面*/
  if(to.fullPath == "/login"){
   if(localStorage.getItem('tokenStr')){
    next({
     path:from.fullPath
    });
   }else {
    next();
   }
  }
 });





new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
